package com.ctshk.rpc.order.bus.service.impl;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.service.support.BaseService;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.order.bus.dto.BusOrderCollectionDTO;
import com.ctshk.rpc.order.bus.dto.BusOrderDTO;
import com.ctshk.rpc.order.bus.dto.BusOrderReceiptInfoDTO;
import com.ctshk.rpc.order.bus.dto.BusOrderTouristDTO;
import com.ctshk.rpc.order.bus.entity.BusOrderReceipt;
import com.ctshk.rpc.order.bus.mapper.BusOrderCollectionMapper;
import com.ctshk.rpc.order.bus.mapper.BusOrderMapper;
import com.ctshk.rpc.order.bus.mapper.BusOrderReceiptMapper;
import com.ctshk.rpc.order.bus.mapper.BusOrderTouristMapper;
import com.ctshk.rpc.order.bus.req.BusOrderReceiptReq;
import com.ctshk.rpc.order.bus.service.IBusOrderReceiptService;

/**
 * <p>
 * 巴士订单收据 服务实现类
 * </p>
 *
 * @author 彭柳霖
 * @since 2021-03-18
 */
@DubboService
public class BusOrderReceiptServiceImpl extends ServiceImpl<BusOrderReceiptMapper, BusOrderReceipt> implements IBusOrderReceiptService, BaseService {

    @Autowired
    private BusOrderReceiptMapper busOrderReceiptMapper;
    
    @Autowired
    BusOrderCollectionMapper busOrderCollectionMapper;
    
    @Autowired
    BusOrderMapper busOrderMapper;
    
    @Autowired
    BusOrderTouristMapper busOrderTouristMapper;
    
    /**
     * 保存订单收据
     * @param busOrderReceiptReq
     * @param user
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result saveTrainReceipt(BusOrderReceiptReq busOrderReceiptReq, TokenUser user) {
        BusOrderReceipt busOrderReceipt = busOrderReceiptMapper.selectOne(new QueryWrapper<BusOrderReceipt>().lambda().eq(BusOrderReceipt::getOrderId, busOrderReceiptReq.getOrderId()));

        if(Objects.isNull(busOrderReceipt)){
            //新增
            busOrderReceipt.setId(SnowflakeIdWorker.nextId());
            busOrderReceipt= EntityUtil.copy(busOrderReceiptReq, BusOrderReceipt.class);
            busOrderReceipt.setGmtCreate(LocalDateTime.now());
            busOrderReceipt.setCreateId(user.getId());
            busOrderReceiptMapper.insert(busOrderReceipt);
        }else{
            //修改
            busOrderReceipt= EntityUtil.copy(busOrderReceiptReq, BusOrderReceipt.class);
            busOrderReceipt.setModifiedId(user.getId());
            busOrderReceipt.setGmtModified(LocalDateTime.now());
            busOrderReceiptMapper.updateById(busOrderReceipt);
        }

        return Result.success();
    }

    /**
     * 根据订单id查询订单详细信息
     *
     * @param orderId 订单id
     * @return 订单消息
     */
    @Override
    public Result<BusOrderReceiptInfoDTO> queryOrderReceiptInfo(Long orderId) {
    	BusOrderReceiptInfoDTO dto = busOrderReceiptMapper.assertExistSelectOneByKey(BusOrderReceiptInfoDTO.class, "order_id", orderId);
    	
    	//订单
    	BusOrderDTO orderDto = busOrderMapper.selectById(BusOrderDTO.class, orderId);
    	dto.setOrderDto(orderDto);;
        
    	//收款列表
    	List<BusOrderCollectionDTO> collectionList = busOrderCollectionMapper.selectListByKey(BusOrderCollectionDTO.class, "order_id", orderId);
    	dto.setCollectionList(collectionList);
    	
    	//旅客列表
    	List<BusOrderTouristDTO> touristList = busOrderTouristMapper.selectListByKey(BusOrderTouristDTO.class, "order_id", orderId);
    	dto.setTouristList(touristList);
    	
        return newResult(dto);
    }
    
    /**
     * 收据发送邮件
     * @param orderId
     * @return
     */
    @Override
    public Result<Void> sendEmailReceipt(Long orderId) {
    	
    	return newResult();
    }
}
